package bairui.international.console.web.controller.product.category;

import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;
import com.querydsl.core.BooleanBuilder;

import bairui.international.console.web.controller.product.category.product.C_Product_Category_ProductShow_ShowImg;
import bairui.international.model.domain.jpa.QTB_Product;
import bairui.international.model.domain.jpa.TB_Product;
import bairui.international.model.domain.jpa.TB_ProductPropertyItem;
import bairui.international.service.Service_Product;
import bairui.international.service.Service_ProductPropertyItem;

/**
 *
 * 详情
 *
 * @generated
 * 
 * @see STest_C_Product_Category_Detail_ProductPage 测试
 *
 */
@Controller
public class C_Product_Category_Detail_ProductPage
 extends SC_Product_Category_Detail_ProductPage 
{
	
	@Autowired private Service_Product service_Product;
	@Autowired private Service_ProductPropertyItem service_ProductPropertyItem;
	
	/**
	 * 物料列表
	 */
	@Action(ProductPage.class)
	@GetMapping(value=ProductPage.RequestMappingValue, consumes=MediaType.APPLICATION_JSON_VALUE)
	public @FomatOutput @ResponseBody Object productPage(
		HttpServletRequest request, 
		@PathVariable(name=ProductPage.PathVariableName.Id) long id
	) throws Exception {
		org.springframework.data.domain.Page<TB_Product> page = service_Product.page(
				it->filter(it, id, request), 
				ProductPage.Output.page(request), 
				it->sort(it, request)
			);
		//
		page.forEach(it->{
			it.setImgUrl(new C_Product_Category_ProductShow_ShowImg.ShowImg.Operate(){
				@Override public Object getCategoryId() {
					return id;
				}
				@Override public Object getId() {
					return it.getId();
				}
			}.getUrl());
			//
			gainProperty(it, service_ProductPropertyItem);
		});
		//
		return ProductPage.Output.output(
				page
			);
	}
	

	public static void gainProperty(TB_Product product, Service_ProductPropertyItem service_ProductPropertyItem){
		product.setProductPropertyItems(
				service_ProductPropertyItem
					.list(it3->it3.products.any().eq(product))
					.stream()
					.map(it3->{
						TB_ProductPropertyItem item = new TB_ProductPropertyItem();
							item.setName(it3.getName());
						return item;
					})
					.collect(Collectors.toList())
			);
	}

	public static Sort sort(QTB_Product it, HttpServletRequest request) {
		return ProductPage.Output.sort(it, request, new ProductPage.Output.OrderExtends() {
			@Override public Sort gainProductCategory(Direction direction) {
				return sort(direction, it.productCategory.name);
			}
			@Override public Sort gainProductPropertyItems(Direction direction) {
				return sort(direction, it.productPropertyItems.any().name);
			}
			@Override public Sort gainWineBottleNormsWine(Direction direction) {
				return sort(direction, it.wineBottleNormsWine.name);
			}
		});
	}

	public static BooleanBuilder filter(QTB_Product it, long id, HttpServletRequest request) {
		return ProductPage.Filter.query(it, request)
				.and(it.productCategory.id.eq(id));
	}
	
}
